/**
 *
 *
 *
 * **/
#include <string.h>  
#include <boost/asio.hpp>  
#include <boost/bind.hpp>  
#include <boost/shared_ptr.hpp>  
#include <boost/enable_shared_from_this.hpp>  
  
using boost::asio::ip::tcp;  
using boost::asio::ip::address;  
  
class session : public boost::enable_shared_from_this<session> {  
public:  
    session(boost::asio::io_service &io_service) : socket_(io_service)  
    {  
    }  
  
    void start() {  
        static tcp::no_delay option(true);  
        socket_.set_option(option);  
  
        boost::asio::async_read_until(socket_,  
            sbuf_,  
            "\n",  
            boost::bind(&session::handle_read,  
            shared_from_this(),  
            boost::asio::placeholders::error,  
            boost::asio::placeholders::bytes_transferred));  
    }  
  
    tcp::socket &socket() {  
        return socket_;  
    }  
  
private:  
    void handle_write(const boost::system::error_code& error, size_t bytes_transferred) {  
        boost::asio::async_read_until(socket_,  
            sbuf_,  
            "\n",  
            boost::bind(&session::handle_read,  
            shared_from_this(),  
            boost::asio::placeholders::error,  
            boost::asio::placeholders::bytes_transferred));  

		
    }  
  
    void handle_read(const boost::system::error_code& error, size_t bytes_transferred) {  
        boost::asio::async_write(socket_,  
            sbuf_,  
            boost::bind(&session::handle_write,  
            shared_from_this(),  
            boost::asio::placeholders::error,  
            boost::asio::placeholders::bytes_transferred));
		
			//print received message
			boost::asio::streambuf::const_buffers_type cbt = sbuf_.data();
			std::string sbuf_data(boost::asio::buffers_begin(cbt), boost::asio::buffers_end(cbt));
        	//std::cout << buf << std::endl;  
        	std::cout << "zewens:" << std::endl;  
        	std::cout << sbuf_data << std::endl; 
  
    }  
  
private:  
    tcp::socket socket_;  
    boost::asio::streambuf sbuf_;  
};  
  
typedef boost::shared_ptr<session> session_ptr;  
  
class server {  
public:  
    server(boost::asio::io_service &io_service, tcp::endpoint &endpoint)  
        : io_service_(io_service), acceptor_(io_service, endpoint)  
    {  
        session_ptr new_session(new session(io_service_));  
        acceptor_.async_accept(new_session->socket(),  
            boost::bind(&server::handle_accept,  
            this,  
            new_session,  
            boost::asio::placeholders::error));  
    }  
  
    void handle_accept(session_ptr new_session, const boost::system::error_code& error) {  
        if (error) {  
            return;  
        }  

		//打印连接的client端的ip地址
  		std::cout <<new_session->socket().remote_endpoint().address() << std::endl;

        new_session->start();  
        new_session.reset(new session(io_service_));  
        acceptor_.async_accept(new_session->socket(),boost::bind(&server::handle_accept,this,new_session,  
            boost::asio::placeholders::error));  
    }  
  
    void run() {  
        io_service_.run();  
    }  
  
private:  
    boost::asio::io_service &io_service_;  
    tcp::acceptor acceptor_;  
};  
  
int main(int argc, char* argv[])  
{  
    boost::asio::io_service io_service;  
    tcp::endpoint endpoint(tcp::v4(), 1000);  
  
    server s(io_service, endpoint);  
    s.run();  
    return 0;  
}  
